package com.zllkj.mm.module.goods

import android.annotation.SuppressLint
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.zllkj.mm.MyApp
import com.zllkj.mm.R
import com.zllkj.mm.adapter.FragmentBaseAdapter
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.AppUtils
import kotlinx.android.synthetic.main.activity_order_list.view.*
import kotlinx.android.synthetic.main.activity_order_list.view.vp_order_list
import kotlinx.android.synthetic.main.fragment_nearby_goods_list.view.*

class NearByGoodsListFragment : BaseFragment() {
    private val titles = arrayOf("附近", "关注")
    private val nearByListItemFragments = arrayListOf<NearByGoodsListItemFragment>()
    var curPagePosition = 0
    override fun getLayoutId(): Int {
        return R.layout.fragment_nearby_goods_list
    }

    @SuppressLint("RestrictedApi")
    override fun loadData() {
        super.loadData()


        nearByListItemFragments.add(NearByGoodsListItemFragment(0,"",AppUtils.lng,AppUtils.lat))
        nearByListItemFragments.add(NearByGoodsListItemFragment(1,"",AppUtils.lng,AppUtils.lat))

        mRootView?.findViewById<TabLayout>(R.id.tab_layout_goods_list)?.let { tablayout ->
            titles.mapIndexed { index, s -> mRootView?.tab_layout_goods_list?.addTab(tablayout.newTab().setText(s))}
        }

        mRootView?.vp_nearby_goods_list_view_pager?.apply {
            adapter= FragmentBaseAdapter(childFragmentManager, nearByListItemFragments, titles)
            addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
                override fun onPageSelected(position: Int) {
                    curPagePosition = position
                    mRootView?.tab_layout_goods_list?.let {
                        it.setScrollPosition(position,0f,false)
                    }
                }
            })
        }
        mRootView.ed_goods_list_search.setOnEditorActionListener { v, actionId, event ->
            nearByListItemFragments[curPagePosition].datas.clear()
            nearByListItemFragments[curPagePosition].loadListData( mRootView.ed_goods_list_search.text.toString(), OrderListItemFragment.Companion.PULL_DOWN)
            true
        }

        mRootView?.tab_layout_goods_list?.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                mRootView?.vp_nearby_goods_list_view_pager?.currentItem = tab.position
                curPagePosition = tab.position
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })

    }

}